package com.bxl.netty.rpc.common.builder;



import com.bxl.netty.rpc.common.message.PackageMsg;
import com.bxl.netty.rpc.common.message.RegisterContent;
import com.bxl.netty.rpc.common.message.RpcContent;
import com.bxl.netty.rpc.common.message.RpcHeader;

import java.util.UUID;

/**
 * <>
 *
 * @author baixl
 * @date 2021/2/24
 */
public class MsgBuilder {
    public static PackageMsg<RpcContent> createMsg(String serviceName, String methodName, Object[] args, Class<?>[] parameterTypes) {
        int f = 0x14141414;
        long messageID =  Math.abs(UUID.randomUUID().getLeastSignificantBits());
        RpcHeader rpcHeader = new RpcHeader(f, messageID);
        RpcContent rpcContent = new RpcContent(serviceName, methodName, parameterTypes, args);
        PackageMsg<RpcContent> msg = new PackageMsg<RpcContent>();
        msg.setHeader(rpcHeader);
        msg.setContent(rpcContent);
        return msg;
    }

    public static PackageMsg<RpcContent> createMsg(int flag, long requestID,RpcContent content) {
        RpcHeader rpcHeader = new RpcHeader(flag, requestID);
        PackageMsg<RpcContent> msg = new PackageMsg<RpcContent>();
        msg.setHeader(rpcHeader);
        msg.setContent(content);
        return msg;
    }

    public static PackageMsg<RegisterContent> createMsg(int flag, long requestID, RegisterContent content) {
        RpcHeader rpcHeader = new RpcHeader(flag, requestID);
        PackageMsg<RegisterContent> msg = new PackageMsg<RegisterContent>();
        msg.setHeader(rpcHeader);
        msg.setContent(content);
        return msg;
    }

    public static <T> PackageMsg<T> createMsg(int flag, long requestID, T content) {
        RpcHeader rpcHeader = new RpcHeader(flag, requestID);
        PackageMsg<T> msg = new PackageMsg<T>();
        msg.setHeader(rpcHeader);
        msg.setContent(content);
        return msg;
    }
    
    
    public static PackageMsg<RegisterContent> createMsg(int flag,String app,String ip,int port) {
        long messageID =  Math.abs(UUID.randomUUID().getLeastSignificantBits());
        RpcHeader rpcHeader = new RpcHeader(flag, messageID);
        RegisterContent rpcContent = new RegisterContent(app, ip, port);
        PackageMsg<RegisterContent> msg = new PackageMsg<RegisterContent>();
        msg.setHeader(rpcHeader);
        msg.setContent(rpcContent);
        return msg;
    }
}
